/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import model.entities.NewsType;
import model.dao.services.NewsTypeDAOServices;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author THANMINH
 */
public class NewsTypeDAO implements NewsTypeDAOServices {

    private static NewsTypeDAO daonewtype;
    private Connection conn;
    private Statement st;
    private ResultSet rs;

    public static NewsTypeDAO getInstance() {
        if (daonewtype == null) {
            daonewtype = new NewsTypeDAO();
        }
        return daonewtype;
    }

    @Override
    public List<NewsType> GetAll() {
        if (conn == null) {
            try {
                conn = ConnectionFactory.getConnection();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(NewsTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(NewsTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        List listnew = new ArrayList();
        String sql = ("select * from tblNewsType");
        try {
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            NewsType entity = null;
            while (rs.next()) {
                entity = new NewsType();
                entity.setIdNewType(rs.getInt("idNewType"));
                entity.setName(rs.getString("name"));
                entity.setDescriptions(rs.getString("descriptions"));
                entity.setIsActived(rs.getBoolean("IsActived"));
                listnew.add(entity);
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return listnew;
    }

    @Override
    public NewsType getById(int id) {
        if (conn == null) {
            try {
                conn = ConnectionFactory.getConnection();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(NewsTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(NewsTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        List listnew = new ArrayList();
        String sql = ("select * from tblNewsType where idNewType = " + id);
        NewsType entity = null;
        try {
            st = conn.createStatement();
            rs = st.executeQuery(sql);

            while (rs.next()) {
                entity = new NewsType();
                entity.setIdNewType(rs.getInt("idNewType"));
                entity.setName(rs.getString("name"));
                entity.setDescriptions(rs.getString("descriptions"));
                entity.setIsActived(rs.getBoolean("IsActived"));

            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return entity;
    }

    @Override
    public NewsType getByName(String Name) {
        if (conn == null) {
            try {
                conn = ConnectionFactory.getConnection();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(NewsTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(NewsTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        List listnew = new ArrayList();
        String sql = ("select * from tblNewsType where name = '" + Name + "'");
        NewsType entity = null;
        try {
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            while (rs.next()) {
                entity = new NewsType();
                entity.setIdNewType(rs.getInt("idNewType"));
                entity.setName(rs.getString("name"));
                entity.setDescriptions(rs.getString("descriptions"));
                entity.setIsActived(rs.getBoolean("IsActived"));

            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return entity;
    }

    @Override
    public NewsType getByDescriptions(String descriptions) {
        if (conn == null) {
            try {
                conn = ConnectionFactory.getConnection();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(NewsTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(NewsTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        List listnew = new ArrayList();
        String sql = ("select * from tblNewsType where name = '" + descriptions + "'");
        NewsType entity = null;
        try {
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            while (rs.next()) {
                entity = new NewsType();
                entity.setIdNewType(rs.getInt("idNewType"));
                entity.setName(rs.getString("name"));
                entity.setDescriptions(rs.getString("descriptions"));
                entity.setIsActived(rs.getBoolean("IsActived"));

            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return entity;
    }


    @Override
    public boolean addNewType(NewsType newtype) {
        try {
            if (conn == null) {

                conn = ConnectionFactory.getConnection();

            }
            String sql = "INSERT INTO tblNewsType values(?,?,?)" ;
            PreparedStatement pr = conn.prepareStatement(sql);
            
            pr.setString(1, newtype.getName());
            pr.setString(2, newtype.getDescriptions());
            pr.setBoolean(3, newtype.isIsActived());
            
            
            pr.executeUpdate();
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(NewsTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return true;

    }

    @Override
    public boolean editNewType(NewsType newtype) {
        try {
            if (conn == null) {

                conn = ConnectionFactory.getConnection();

            }
            String sql = "update tblNewsType set [name]= ?, [descriptions] = ? , [isActived] = ? where [idNewType] =" + newtype.getIdNewType();
            PreparedStatement pr = conn.prepareStatement(sql);
            
            pr.setString(1, newtype.getName());
            pr.setString(2, newtype.getDescriptions());
            pr.setBoolean(3, newtype.isIsActived());
            
            
            pr.executeUpdate();
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(NewsTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return true;
    }

    @Override
    public boolean delete(int id) {

        try {
            String sql = ("delete tblNewsType where [idNewType] =" + id);
            if (conn == null) {
                conn = ConnectionFactory.getConnection();
            }
            st = conn.createStatement();
            ResultSet executeQuery = st.executeQuery(sql);

        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    @Override
    public boolean remove(int id) {
        try {
            if (conn == null) {
                conn = ConnectionFactory.getConnection();
            }
            String sql = "update tblNewsType set [isActived] = 0 where [idNewType] = " + id;
            st = conn.createStatement();
            st.executeQuery(sql);

        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;

    }

    @Override
    public boolean restore(int id) {
        try {
            if (conn == null) {
                conn = ConnectionFactory.getConnection();
            }
            String sql = "update tblNewsType set [isActived] = 1 where [idNewType] = " + id;
            st = conn.createStatement();
            st.executeQuery(sql);

        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewsTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

}
